package dao.impl;

import dao.UserInfoDao;
import util.BaseDao;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author ：Teacher陈（86521760@qq.com）
 * @date ：Created in 2022/3/23 9:43
 * @description：用户信息dao实现类
 * @modified By：
 * @version: 1
 */
public class UserInfoDaoImpl  extends BaseDao implements UserInfoDao {
    @Override
    public List<Map<String, Object>> findUserInfoByUserNameAndPassword(String username, String password) {
        String sql="select * from userinfo where username=?  and password=?";
        return executeQuery(sql,username,password);
    }

    @Override
    public List<Map<String, Object>> findUserInfoById(Integer id) {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return executeQuery("select * from userinfo where id=?",id);
    }

    //21--30(limit 10 page 3)
    //(page-1)*limit=20 start
    //  limit  a,b   a是起始位置，不包含a,b是显示多少条
    @Override
    public List<Map<String, Object>> findAllUser() {

        return executeQuery("select * from userinfo");
    }

    @Override
    public List<Map<String, Object>> findAllUserByCondition(Map conditionMap) {
        String sql="select * from userinfo";
        if(null!=conditionMap.get("page")){
           int page=Integer.parseInt( conditionMap.get("page")+"");
           int limit=Integer.parseInt(conditionMap.get("limit")+"");
            sql+=" limit "+(page-1)*limit+" , "+limit;
        }
        System.out.println("=======sql============："+sql);
        return executeQuery(sql);
    }

    @Override
    public boolean deleteUserInfoById(Integer id) {
        return executeUpdate("delete from   userinfo where id=?",id);
    }

    @Override
    public boolean addUserInfo(Map map) {
        String sql="insert into userinfo (username,password ,createtime,phone) values (?,?,?,?)";
        Boolean update = executeUpdate(sql, map.get("username"), map.get("password"), new Date(), map.get("phone"));
        return update;
    }

    @Override
    public boolean updateUserInfo(Map map) {
        String sql="update  userinfo set username=?,password=? ,createtime=?,phone=? where id=?";
        Boolean update = executeUpdate(sql, map.get("username"), map.get("password"), new Date(), map.get("phone"),map.get("id"));
        return update;
}

    @Override
    public Long getCount() {
        String sql="select  count(*) mycount from userinfo";
        List<Map<String, Object>> mapList = executeQuery(sql);
        Map<String, Object> map = mapList.get(0);
        Long mycount = Long.parseLong(map.get("mycount")+"") ;
        return mycount;
    }
}
